/**
 * 2016/03/22 yc 重写sql-hint.js
 * @param {Object} mod
 */
(function(mod) {
    if (typeof exports == "object" && typeof module == "object") // CommonJS
        mod(require("../../lib/codemirror"), require("../../mode/sql/sql"));
    else if (typeof define == "function" && define.amd) // AMD
        define(["../../lib/codemirror", "../../mode/sql/sql"], mod);
    else // Plain browser env
        mod(CodeMirror);
})(function(CodeMirror) {
    "use strict";
    var Pos = CodeMirror.Pos;
    /**
     * 从sql.js里获取keyword数组
     * @param {Object} editor
     */
    function getKeywords(editor) {
        var mode = editor.doc.modeOption;
        if (mode === "sql") mode = "text/x-sql";
        return CodeMirror.resolveMode(mode).keywords;
    };
    /**
     * 判断元素item是否存在数组arr中  
     * @param {Object} arr
     * @param {Object} item
     */
    function arrayContains(arr, item) { // 判断元素item是否存在数组arr中  
        if (!Array.prototype.indexOf) {
            var i = arr.length;
            while (i--) {
                if (arr[i] === item) {
                    return true;
                }
            }
            return false;
        }
        return arr.indexOf(item) != -1;
    };
     
    function hintSql(editor, keywords, tableKeywords, getToken, options) { // 处理hint的核心函数，改名为velocityHint(也可以不做修改)  
        // Find the token at the cursor，获取当前光标指定的字符串  
        var cur = editor.getCursor();
        var token = getToken(editor, cur);
        return {
            list: getCompletions(editor, token, keywords, tableKeywords, options),
            from: Pos(cur.line, fetchStartPoint(token)), // 字符串拼接的初始位置，这个很重要  
            to: Pos(cur.line, token.end)
        };
    };
    /**
     * 字符拼接位置
     * @param {Object} token
     */
    function fetchStartPoint(token) {
        var index = token.string.lastIndexOf("\.");
        if (index < 0) {
            return token.start;
        } else {
            return token.start + index + 1;
        }
        //      return token.start;
    };
 
//    function sqlHint(editor, options) {
//        var keywords = wordToString(getKeywords(editor)) + CodeMirror.keywords;
//        return hintSql(editor, keywords, CodeMirror.tableKeywords, function(e, cur) {
//            return e.getTokenAt(cur);
//        }, options);
//    };
    CodeMirror.registerHelper("hint", "sql", sqlHint);
    
    /**
     * 得到匹配的关键字数组
     * @param {Object} token
     * @param {Object} keywords
     * @param {Object} tableKeywords
     * @param {Object} options
     */
    function getCompletions(editor, token, keywords, tableKeywords, options) {
        var found = [], start;
        var content = getWord(token.string, token.end); // found为匹配的数组  
        if (content && content == "*") {
        	return ["* from "];
        }
        if (content && content.indexOf("*") >= 0 || !content.match(RegExp(/[a-zA-Z_][a-zA-Z0-9_]/))) {
        	return [];
        }
        if (content && content.length) {
            start = token.string.charAt(0); //字符串首字母
            content = content.trim().substring(0, content.length); //除首字母外的截取
        }
        
        var cur = editor.getCursor();
        var line = editor.getLineTokens(cur.line);
        var tableName = "";
        var hasFrom = false
        if (line && line.length > 1) {
	        for (var i=0; i< line.length; i++) {
	        	if (line[i].string == "from") {
	        		hasFrom = true;
	        	}else if (hasFrom && line[i].string.trim() != '') {
		        	hintTableFields = true;
	        		tableName = line[i].string;
	        		break;
	        	}
	        }
        }
        var hintTableFields = tableName != "";
        
        var result = null;
        if (start && start.trim() != '') {
            var regexp = new RegExp("\\b" + tableName + "\\." + content + "\\w+\\.?\\b", "gi");
            if (hintTableFields && tableKeywords) {
                result = tableKeywords.match(regexp);
            } else {
                result = keywords.match(regexp);
            }
            console.log('result = ' + result);
        } else if (start) {
        	var regexp = new RegExp("\\b" + tableName + "\\.\\w+\\.?\\b", "gi");
            if (hintTableFields && tableKeywords) {
                result = tableKeywords.match(regexp);
            } else {
                result = keywords.match(regexp);
            }
            console.log('result = ' + result);
        }
        if (result && result.length) {
            for (var i = 0; i < result.length; i++) {
                if (!arrayContains(found, result[i]) && content.length <= result[i].length && hintTableFields) {
                    var hint_text = result[i];
                    var index = hint_text.indexOf(".");
                    hint_text = index < 0 ? hint_text : hint_text.substring(index + 1, result[i].length);
                	found.push(hint_text);
                } else {
                    found.push(result[i]);
                }
            }
        }
        return found;
    };
    
    
    /**
     * 获取当前字符串
     * @param {Object} str 当前行字符串
     * @param {Object} end 结束位置
     */
    function getWord(str, end) {
        return str.substring(str.lastIndexOf(' '), end);
    };
    /**
     * 将wordlist拼成字符串
     * @param {Object} wordlist
     */
    function wordToString(wordlist) {
        var str = '';
        for (var word in wordlist) {
            str += word + ' ';
        }
        return str;
    };
});